import Model from './model'
import _ from 'lodash'
import assert from 'assert-plus'

const controller = {
	getLostList: async (limit) => {
		return await Model.find({type: 0, isDelete: false}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
	},
	getMoreLostList: async (id, limit) => {
		return await Model.find({type: 0, _id: {$lt: id}, isDelete: false}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
	},
	getFoundList: async () => {
		return await Model.find({type: 1}, '-__v -isDelete', {sort: {_id: -1}}).populate('tags')
	},
	getMoreFoundList: async (id, limit) => {
		return await Model.find({type: 1, _id: {$lt: id}}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
	},
	getOneLaf: async (id) => {
		return await Model.findOne({_id: id}, '-__v -isDelete').populate('tags')
	},
	getMyLaf: async (type, userId, limit, id) => {
		if (!id) {
			return await Model.find({type: type, userId: userId, isDelete: false}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
		}
		return await Model.find({type: type, userId: userId, _id: {$lt: id}, isDelete: false}, '-__v -isDelete', {sort: {_id: -1}, limit: limit}).populate('tags')
	},
	createLaf: async (laf) => {
		return await Model.create(laf)
	},
	deleteLaf: async (id) => {
		return await Model.update({_id: id}, {$set: {isDelete: true}})
	}
};

export default controller;